<?php
/**
 * User:chendenan
 * Created On 2022/3/18 0018 22:52
 */
namespace app\common\business;
use app\common\model\mysql\SpecsValue as SpecsValueModel;
class SpecsValue extends BusBase {
    public $model = null;
    public function __construct(){
        $this->model = new SpecsValueModel();
    }

    public function getBySpecsId($specsId){
        try {
            $result = $this->model->getBySpecsId($specsId,"id,name");
        }catch (\Exception $e){
            return [];
        }
        $result = $result->toArray();
        return $result;
    }
    public function detailGoodsSkus($gids,$flagValue){
        $specsValueKeys = array_keys($gids);
        foreach ($specsValueKeys as $specsValueKey){
            $specsValueKey =  explode(",",$specsValueKey);
            foreach ($specsValueKey as $k=>$v){
                $new[$k][] = $v;
                $specsValueIds[] = $v;
            }
        }
        $specsValueIds = array_unique($specsValueIds);
        $specsValues = $this->getInIds($specsValueIds);

        $flagValue = explode(",",$flagValue);
        $result = [];
        foreach ($new as $key=>$newValue){
            $newValue = array_unique($newValue);
            $list = [];
            foreach($newValue as $vv){
                $list[] = [
                    "id" => $vv,
                    "name" => $specsValues[$vv]['name'],
                    "flag" => in_array($vv,$flagValue) ? 1 : 0,
                ];
            }
            $result[$key] = [
                "name" => $specsValues[$newValue[0]]['specs_name'],
                "list" => $list,
            ];
        }
        return $result;
    }
    public function getInIds($ids){
        if(!$ids){
            return [];
        }
        try {
            $result = $this->model->getInIds($ids);
        }catch(\Exception $e){
            trace($e->getMessage(),"error");
            return [];
        }
        $result = $result->toArray();
        if(!$result){
            return [];
        }
        $specsNames = config("specs");
        $specsNamesArrs = array_column($specsNames,"name","id");

        $res = [];
        foreach ($result as $resultValue){
            $res[$resultValue['id']] = [
                "name" => $resultValue["name"],
                "specs_name" => $specsNamesArrs[$resultValue['specs_id']] ?? "" ,
            ];
        }
        return $res;
    }
    public function detailSpecsValue($skuIdSpecsValueIds){
        $ids = array_values($skuIdSpecsValueIds);
        $ids = implode(",",$ids);
        $ids = array_unique(explode(",",$ids));
        $result = $this->getInIds($ids);
        if(!$result){
            return [];
        }
        $res = [];
        foreach ($skuIdSpecsValueIds as $skuId => $specs){
            $specs = explode(",",$specs);
            //处理SKU默认文案
            $skuStr = [];
            foreach ($specs as $spec){
                $skuStr[] = $result[$spec]['specs_name'].":".$result[$spec]['name'];
            }
            $res[$skuId] = implode("  ",$skuStr);
        }
        return $res;
    }
}